package com.huawei.su;

/**
 * @Author: liu.cheng.su
 * @Time: 2023/10/4 0004 21:26
 * @Description:
 */
public class BM15 {

    public static void main(String[] args) {
        ListNode listNode1 = new ListNode(1);
        ListNode listNode2 = new ListNode(1);
        ListNode listNode3 = new ListNode(2);
        listNode1.next = listNode2;
        listNode2.next = listNode3;

        deleteDuplicates(listNode1);
    }

    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     * @param head ListNode类
     * @return ListNode类
     */
    public static ListNode deleteDuplicates(ListNode head) {
        // write code here
        if (head == null || head.next == null) {
            return head;
        }
        ListNode indexBefore = head;
        ListNode indexAfter = indexBefore.next;
        int flag = 0;
        while (true) {
            if (indexAfter == null) {
                if (flag == 1) {
                    indexBefore.next = indexAfter;
                }
                break;
            }
            if (indexBefore.val == indexAfter.val) {
                indexAfter = indexAfter.next;
                flag = 1;
                continue;
            }
            if (flag == 1) {
                indexBefore.next = indexAfter;
            }
            indexBefore = indexBefore.next;
            indexAfter = indexAfter.next;
        }
        return head;
    }
}
